package com.course.cdspringboot.mapper.achievement;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.course.cdspringboot.entity.achievement.Achievements;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AchievementsMapper extends BaseMapper<Achievements> {

    @Select("SELECT achievement_id FROM achievements "
            + "WHERE owner = #{owner} "
            + "AND achievement_type_id = #{achievementTypeId} "
            + "AND is_deleted = false")
    List<Integer> getAchievementIdsByUser(long owner, int achievementTypeId);

    // 查看教师是否有权限修改这个成果
    @Select("SELECT count(*) FROM achievements " +
            "WHERE  achievement_id = #{achievementId} AND owner IN (SELECT uid FROM students WHERE teacher_id = #{uid}) AND is_deleted = false")
    int isTeacherOwnAchievement(Long uid, Integer achievementId);

    // 查看学生是否有这个成果
    @Select("SELECT count(*) FROM achievements " +
            "WHERE owner = #{owner} AND achievement_id = #{achievement_id} AND is_deleted = false")
    int isUserOwnAchievement(Long owner, Integer achievement_id);


    /* 学生删除成果 */
    @Delete("UPDATE achievements set is_deleted = true WHERE achievement_id = #{achievement_id}")
    void deleteAchievement(int achievement_id);


    /* 学生插入成果 */
    @Insert("INSERT INTO achievements (owner, achievement_type_id) " +
            "VALUES (#{owner}, #{achievement_type_id})")
    @Options(useGeneratedKeys = true, keyProperty = "achievement_id")
    void insertAchievements(Map<String, Object> insert_info);

    // 设置审核状态
    @Update("UPDATE achievements SET review_status_id = #{review_status_id}, review_comments = #{review_comments} WHERE achievement_id = #{achievementId}")
    int updateReviewStatus(int achievementId, int review_status_id, String review_comments);

    // 查看成果是否已经审核通过
    @Select("SELECT count(*) FROM achievements WHERE achievement_id = #{achievement_id} AND review_status_id = 3")
    int isAchievementPassed(int achievement_id);
}
